package com.yyydjk.library.activitys;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.material_design_iconic_typeface_library.MaterialDesignIconic;
import com.yyydjk.library.R;
import com.yyydjk.library.base.BaseActivity;
import com.yyydjk.library.theme.ColorView;
import com.yyydjk.library.utils.SystemUtils;
import com.yyydjk.library.utils.ThemeUtils;
import com.yyydjk.library.value.NetValue;
import com.yyydjk.library.widget.avi.AVLoadingIndicatorView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/10 0010.
 */

public class WebViewActivityy extends BaseActivity{
    @Bind(R.id.mwebview)
    WebView mWebView;
    @Bind(R.id.loading_animate)
    AVLoadingIndicatorView avLoadingIndicatorView;
    @Bind(R.id.status_bar)
    ColorView mStatusBar;
    @Bind(R.id.icon)
    ImageView mIcon;
    @Bind(R.id.title)
    TextView mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mStatusBar.setVisibility(View.VISIBLE);
            mStatusBar.getLayoutParams().height = SystemUtils.getStatusHeight(this);
            mStatusBar.setLayoutParams(mStatusBar.getLayoutParams());
        } else {
            mStatusBar.setVisibility(View.GONE);
        }
        initView();
        initDatas();
    }

    private void initDatas() {
        Intent intent = getIntent();
        String classtype = intent.getStringExtra(NetValue.MLIBRARY_CLASSTYPE);
        String libraryName = intent.getStringExtra(NetValue.MLIBRARY_LIBRARYNAME);
        String libraryUrl = intent.getStringExtra(NetValue.MLIBRARY_LIBRARYURL);
        mTitle.setText(libraryName);
        switch (classtype){
            case NetValue.LIBRARY_CLASSTYPE_ANDROID:
                mIcon.setImageDrawable(new IconicsDrawable(this).color(Color.WHITE).icon(MaterialDesignIconic.Icon.gmi_android).sizeDp(20));
                break;
            case NetValue.LIBRARY_CLASSTYPE_APP:
                mIcon.setImageDrawable(new IconicsDrawable(this).color(Color.WHITE).icon(MaterialDesignIconic.Icon.gmi_apps).sizeDp(20));
                break;
            case NetValue.LIBRARY_CLASSTYPE_QD:
                mIcon.setImageDrawable(new IconicsDrawable(this).color(Color.WHITE).icon(MaterialDesignIconic.Icon.gmi_language_javascript).sizeDp(20));
                break;
        }
        mWebView.loadUrl(libraryUrl);
    }

    private void initView() {
        //声明WebSettings子类
        WebSettings webSettings = mWebView.getSettings();

//如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);

//设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

//缩放操作
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

//其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                new MaterialDialog.Builder(WebViewActivityy.this)
                        .title(R.string.about)
                        .icon(new IconicsDrawable(WebViewActivityy.this)
                                .color(ThemeUtils.getThemeColor(WebViewActivityy.this, R.attr.colorPrimary))
                                .icon(MaterialDesignIconic.Icon.gmi_info)
                                .sizeDp(20))
                        .content(message)
                        .positiveText("确定")
                        .show();
                //注意:
                //必须要这一句代码:result.confirm()表示:
                //处理结果为确定状态同时唤醒WebCore线程
                //否则不能继续点击按钮
                result.confirm();
                return true;
            }
        });
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                avLoadingIndicatorView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                avLoadingIndicatorView.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()){//点击返回按钮的时候判断有没有上一页
            mWebView.goBack(); // goBack()表示返回webView的上一页面
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWebView.destroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mWebView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mWebView.onResume();
    }
}
